#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstdint>

#include <sys/timerfd.h>

int main()
{

    // 创建一个定时器

    int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);

    struct itimerspec item;

    item.it_value.tv_sec = 3;  // 设置第一次超时时间
    item.it_value.tv_nsec = 0; // 将纳秒设置为0，避免随机数

    item.it_interval.tv_sec = 2; // 设置第一次之后超时时间
    item.it_interval.tv_nsec = 0;

    // 启动定时器

    timerfd_settime(timerfd, 0, &item, NULL);

    time_t start = time(NULL);

    while (1)
    {
        uint64_t tmp;
        sleep(3);

        int ret = read(timerfd, &tmp, sizeof(tmp));

        if (ret < 0)
        {
            return -1;
        }
        std::cout << tmp << " " << time(NULL) - start << std::endl;
    }

    close(timerfd);

    return 0;
}